1:- module('transpiler', [translate/3,translate/4]).    2:- use_module(parser).    3:- use_module(tokenizer).    4
    5:- set_prolog_flag(double_quotes,chars).    6:- use_module(library(chr)).    7  
    8:- chr_constraint var_type(+,+,+).
    9var_type(Namespace,Var,Type) \ var_type(Namespace,Var,Type) <=> true.
   10var_type(Namespace,Var,Type1),var_type(Namespace,Var,Type2) <=> Type1=Type2. %,writeln('\ntype of'),writeln(Var),writeln('in'),writeln(Namespace),writeln('is'),writeln(Type1).
   11var_type(_,number(_),Type) <=> (Type=number;Type=int).
   12var_type(Namespace,Var,_) ==> type_inference(Namespace,Var).
   13
   14:- chr_constraint return_type(+,+).
   15return_type(A,B) \ return_type(A,B) <=> true.
   16return_type(A,B), return_type(A,C) <=> B=C.
   17
   18:- chr_constraint type_inference(+,+).
   19type_inference(+,+) \ type_inference(+,+) <=> true.
   20 
   21:- chr_constraint var_types(+,+,+).
   22var_types(Namespace,[Var],Type) <=> var_type(Namespace,Var,Type).
   23var_types(Namespace,[Var|Vars],Type) <=> var_type(Namespace,Var,Type),var_types(Namespace,Vars,Type).
   24
   25:- chr_constraint same_var_type(+,+,+).
   26same_var_type(+,+,+) \ same_var_type(+,+,+) <=> true.
   27same_var_type(Namespace,Var1,Var2) ==> var_type(Namespace,Var1,Type),var_type(Namespace,Var2,Type).
   28 
   29:- chr_constraint function(+,+,+).
   30function(A,B,C) \ function(A,B,C) <=> true.
   31function(Type,Name,Params), function(Type1,Name,Params1) ==> Type=Type1,Params=Params1,writeln([function,Name]).
   32
   33:- chr_constraint parse_lang(+,+,+).
   34parse_lang(A,B,C) \ parse_lang(A,B,C) <=> true.
   35parse_lang(Lang,Input,Output) ==>
   36	phrase(parser:statements_(Lang,"\n",Output),Input).
   37
   38 
   39:- chr_constraint same_param_types(+,+,+).
   40same_param_types(A,B,C) \ same_param_types(A,B,C) <=> true.
   41
   42:- chr_constraint type_inference_(+,+).
   43type_inference_(A,B) \ type_inference_(A,B) <=> true.
   44type_inference_(Namespace,[A]) <=>
   45    type_inference(Namespace,A).
   46type_inference_(Namespace,[A|B]) <=>
   47    type_inference(Namespace,A),type_inference_(Namespace,B).
   48
   49translate(Lang2,Input,Output) :-
   50	member(Lang1,[python,php,ruby,javascript,lua,java,c,'c#']),
   51	translate(Lang1,Lang2,Input,Output).
   52	
   53translate(Lang1,Lang2,Input_,Output) :-
   54    append("\n",Input_,Input),parse_lang(Lang1,Input,Output1),writeln('finished parsing'),!,type_inference_('_',Output1),!,writeln('Type inference finished. Generating source code:'),parse_lang(Lang2,Output,Output1).
   55 
   56same_param_types(Namespace,[Params],[Params2]) ==>
   57    Params2 = symbol(Type,_),
   58    var_type(Namespace,Params,Type).
   59same_param_types(Namespace,[A|A1],[B|B1]) ==>
   60    same_param_types(Namespace,[A],[B]),same_param_types(Namespace,A1,B1).
   61
   62%type_inference(Namespace,A) ==>
   63%	writeln(['infer types for',A,'in',Namespace]).
   64
   65type_inference(Namespace,access_array(Type1,Arr,Index)) <=>
   66	var_type(Namespace,Index,int),
   67	writeln(access_array(Type1,Arr,Index)),
   68	(var_type(Namespace,Arr,[array,Type]),
   69	var_type(Namespace,access_array(Type1,Arr,Index),Type),
   70	Type1 = [array,Type];
   71	var_type(Namespace,Arr,string),
   72	var_type(Namespace,access_array(Type1,Arr,Index),char),
   73	Type1 = string).
   74
   75type_inference(Namespace,random_from_list(Arr,Index)) <=>
   76	var_type(Namespace,Arr,[array,Type]),
   77	var_type(Namespace,random_from_list(Arr),Type).
   78
   79
   80type_inference(Namespace,replace(string,A,B,C)) <=>
   81	var_types(Namespace,[A,B,C,replace(string,A,B,C)],string).
   82
   83type_inference(Namespace,set_array_size(Type,Name,Size)) <=>
   84	var_type(Namespace,Size,int),
   85	var_type(Namespace,Name,[array,Type]).
   86
   87type_inference(Namespace,lstrip(A)) <=>
   88	var_types(Namespace,[A,lstrip(A)],string).
   89type_inference(Namespace,rstrip(A)) <=>
   90	var_types(Namespace,[A,rstrip(A)],string).
   91type_inference(Namespace,strip(A)) <=>
   92	var_types(Namespace,[A,strip(A)],string).
   93type_inference(Namespace,substring(A,B,C)) <=>
   94	var_types(Namespace,[A,substring(A,B,C)],string),
   95	var_types(Namespace,[B,C],int).
   96
   97type_inference(Namespace,split(string,A,B)) <=>
   98	var_types(Namespace,[A,B],string),
   99	var_type(Namespace,split(string,A,B),[array,string]).
  100
  101type_inference(Namespace,join(Arr,Sep)) <=>
  102	var_types(Namespace,[Sep,join(Arr,Sep)],string),
  103	var_type(Namespace,Arr,[array,string]).
  104
  105type_inference(Namespace,this(Type,Name)) <=>
  106	Namespace = [_|Namespace1],type_inference(Namespace1,symbol(Type,var_name(Type,Name))).
  107	
  108type_inference(Namespace,symbol(Type,var_name(Type,Name))) <=>
  109	var_type(Namespace,symbol(Type,var_name(Type,Name)),Type),
  110	var_type(Namespace,var_name(Type,Name),Type),
  111	type_inference(Namespace,var_name(Type,Name)).
  112type_inference(Namespace,var_name(Type,Name)) <=>
  113    var_type(Namespace,symbol(Type,var_name(Type,Name)),Type),
  114	var_type(Namespace,var_name(Type,Name),Type).
  115type_inference(Namespace,function_call(Type,Name,Params)) <=>
  116    function(Type,Name,Params1),
  117    var_type(Namespace,function_call(Type,Name,Params),Type),
  118    same_param_types(Namespace,Params,Params1),
  119    type_inference_(Namespace,Params).
  120type_inference(Namespace,randint(A,B)) <=>
  121    var_types(Namespace,[A,B,randint(A,B)],int).
  122type_inference(Namespace,contains(Type1,Container,Contained)) <=>
  123    (Type1=[array,Type];Type1=string,Type=string),
  124    var_type(Namespace,contains(Type1,Container,Contained),bool),
  125    var_type(Namespace,Contained,Type),
  126    var_type(Namespace,Container,Type1),
  127    type_inference_(Namespace,[Container,Contained]).
  128
  129type_inference(Namespace,constructor(Name,Params,Body)) <=>
  130	Namespace = [Name|_],
  131	writeln(Name),
  132	type_inference(Namespace,function(Name,Name,Params,Body)).
  133
  134type_inference(Namespace,static_method(Type,Name,Params,Body)) <=>
  135	type_inference(Namespace,function(Type,Name,Params,Body)).
  136type_inference(Namespace,instance_method(Type,Name,Params,Body)) <=>
  137	type_inference(Namespace,function(Type,Name,Params,Body)).
  138type_inference(Namespace,function(Type,Name,Params,Body)) <=>
  139    function(Type,Name,Params),
  140    type_inference_([Name|Namespace],Body),
  141    type_inference_([Name|Namespace],Params),
  142    return_type([Name|Namespace],Type),
  143    writeln(function(Type,Name,Params)).
  144type_inference(Namespace,class(Name,Body)) <=>
  145    type_inference_([Name|Namespace],Body).
  146type_inference(Namespace,number(A)) <=>
  147    var_type(Namespace,number(A),_).
  148type_inference(Namespace,typeof(_,A)) <=>
  149    var_type(Namespace,typeof(_,A),type).
  150type_inference(Namespace,typeof(A)) <=>
  151    var_type(Namespace,typeof(A),type).
  152type_inference(Namespace,subtract(A,B)) <=>
  153    var_types(Namespace,[A,B,subtract(A,B)],Type),(Type=int;Type=number).
  154type_inference(Namespace,divide(A,B)) <=>
  155    var_types(Namespace,[A,B,divide(A,B)],Type),(Type=int;Type=number).
  156type_inference(Namespace,length(Type,A)) <=>
  157    var_type(Namespace,A,Type),
  158    var_type(Namespace,length(Type,A),int),
  159    (Type = [array,_]; Type=string).
  160type_inference(Namespace,println(Type,A)) <=>
  161    var_type(Namespace,A,Type).
  162type_inference(Namespace,assert(A)) <=>
  163    var_type(Namespace,A,bool).
  164type_inference(Namespace,initializer_list(Type,A)) <=>
  165	var_types(Namespace,A,Type),
  166    var_type(Namespace,initializer_list(Type,A),[array,Type]).
  167    
  168type_inference(Namespace,for(Initialize,Condition,Update,Body)) <=>
  169	var_type(Namespace,Condition,bool),
  170	type_inference_(Namespace,[Initialize,Update]),
  171	type_inference_(Namespace,Body).
  172	
  173type_inference(Namespace,char_literal(A)) <=>
  174    var_type(Namespace,char_literal(A),char).
  175type_inference(Namespace,string_literal(A)) <=>
  176    var_type(Namespace,string_literal(A),string).
  177type_inference(Namespace,not(A)) <=>
  178    var_types(Namespace,[not(A),A],bool).
  179type_inference(Namespace,range(A,B)) <=>
  180    var_types(Namespace,[A,B],int),
  181    var_type(Namespace,range(A,B),[array,int]).
  182type_inference(Namespace,sin(A)) <=>
  183    var_types(Namespace,[sin(A),A],number).
  184type_inference(Namespace,sqrt(A)) <=>
  185    var_types(Namespace,[sqrt(A),A],number).
  186type_inference(Namespace,pow(A,B)) <=>
  187    var_types(Namespace,[pow(A,B),A,B],number).
  188type_inference(Namespace,floor(A)) <=>
  189    var_types(Namespace,[floor(A),A],number).
  190type_inference(Namespace,ceiling(A)) <=>
  191    var_types(Namespace,[ceiling(A),A],number).
  192type_inference(Namespace,abs(Type,A)) <=>
  193    var_types(Namespace,[abs(A),A],Type),
  194    type_inference(Namespace,A).
  195type_inference(Namespace,cos(A)) <=>
  196    var_types(Namespace,[cos(A),A],number).
  197type_inference(Namespace,tan(A)) <=>
  198    var_types(Namespace,[tan(A),A],number).
  199type_inference(Namespace,multiply(A,B)) <=>
  200    var_types(Namespace,[A,B,multiply(A,B)],Type),
  201    (Type=int;Type=number).
  202type_inference(Namespace,greater_than(Type,A,B)) <=>
  203    var_types(Namespace,[A,B],Type),
  204    var_type(Namespace,greater_than(Type,A,B),bool).
  205type_inference(Namespace,less_than(Type,A,B)) <=>
  206    var_types(Namespace,[A,B],Type),
  207    var_type(Namespace,less_than(Type,A,B),bool).
  208type_inference(Namespace,less_than_or_equal(Type,A,B)) <=>
  209    var_types(Namespace,[A,B],Type),
  210    var_type(Namespace,less_than_or_equal(A,B),bool).
  211type_inference(Namespace,greater_than_or_equal(Type,A,B)) <=>
  212    var_types(Namespace,[A,B],Type),
  213    var_type(Namespace,less_than_or_equal(A,B),bool).
  214type_inference(Namespace,plus_plus(Type,A)) <=>
  215    var_type(Namespace,A,Type),
  216    (Type=int;Type=number).
  217type_inference(Namespace,reverse_in_place(Type,A)) <=>
  218    var_type(Namespace,A,Type).
  219type_inference(Namespace,sort_in_place(Type,A)) <=>
  220    var_type(Namespace,A,Type).
  221type_inference(Namespace,parentheses(A)) <=>
  222    var_types(Namespace,[parentheses(A),A],Type).
  223type_inference(Namespace,add(Type,A,B)) <=>
  224    var_types(Namespace,[A,B,add(Type,A,B)],Type).
  225type_inference(Namespace,or(A,B)) <=>
  226    var_types(Namespace,[A,B,or(A,B)],bool).
  227type_inference(Namespace,and(A,B)) <=>
  228    var_types(Namespace,[A,B,and(A,B)],bool).
  229type_inference(Namespace,set_var(Type,A,B)) <=>
  230    var_types(Namespace,[A,B],Type).
  231type_inference(Namespace,return(Type,A)) <=>
  232    return_type(Namespace,Type),
  233    var_type(Namespace,A,Type).
  234type_inference(Namespace,initialize_var(Type,A,B)) <=>
  235    var_types(Namespace,[A,B],Type).
  236type_inference(Namespace,initialize_constant(Type,A,B)) <=>
  237    var_types(Namespace,[A,B],Type).
  238type_inference(Namespace,initialize_constant(Type,A,B)) <=>
  239    var_types(Namespace,[A,B],Type).
  240
  241type_inference(Namespace,plus_equals(Type,A,B)) <=>
  242    var_types(Namespace,[A,B],Type).
  243 
  244type_inference(Namespace,divide_equals(A,B)) <=>
  245    var_types(Namespace,[A,B],number).
  246
  247type_inference(Namespace,type_conversion(Type1,Type2,A)) <=>
  248    writeln('equals type inference'),
  249    var_type(Namespace,A,Type1),
  250    var_type(Namespace,type_conversion(Type1,Type2,A),Type2).
  251type_inference(Namespace,equals(Type,A,B)) <=>
  252    var_types(Namespace,[A,B],Type),
  253    var_type(Namespace,equals(Type,A,B),bool).
  254%set index for dictionaries and arrays
  255type_inference(Namespace,set_array_index(A,B,C)) <=>
  256    var_type(Namespace,B,Type1),var_type(Namespace,C,Type2),var_type(Namespace,A,[dict,Type1,Type2]).
  257type_inference(Namespace,set_array_index(A,B,C)) <=>
  258    var_type(Namespace,B,number),var_type(Namespace,C,Type),var_type(Namespace,A,[array,Type]),
  259    type_inference_(Namespace,[A,B,C]).
  260type_inference(Namespace,not_equals(Type,A,B)) <=>
  261    var_types(Namespace,[A,B],Type),
  262    var_type(Namespace,not_equals(Type,A,B),bool).
  263type_inference(Namespace,'true') <=>
  264	var_type(Namespace,'true',bool).
  265type_inference(Namespace,'false') <=>
  266	var_type(Namespace,'false',bool).
  267type_inference(Namespace,times_equals(A,B)) <=>
  268    var_types(Namespace,[A,B],number).
  269type_inference(Namespace,minus_equals(A,B)) <=>
  270    var_types(Namespace,[A,B],number).
  271     
  272type_inference(Namespace,while(A,B)) <=>
  273    %writeln(['while',namespace,Namespace]),
  274    var_type(Namespace,A,bool),
  275    type_inference_(Namespace,B).
  276
  277type_inference(Namespace,if(A,B)) <=>
  278    %writeln(['if',namespace,Namespace]),
  279    var_type(Namespace,A,bool),
  280    type_inference_(Namespace,B).
  281
  282
  283type_inference(Namespace,if_else(A,B,C)) <=>
  284    %writeln(['if',namespace,Namespace]),
  285    var_type(Namespace,A,bool),
  286    type_inference_(Namespace,B),
  287    type_inference_(Namespace,C).
  288
  289type_inference(Namespace,if(A,B,C,D)) <=>
  290    %writeln(['if',namespace,Namespace]),
  291    var_type(Namespace,A,bool),
  292    type_inference_(Namespace,B),
  293    type_inference_(Namespace,C),
  294    type_inference_(Namespace,D).
  295 
  296type_inference(Namespace,foreach(Type,Var,Array,Body)) <=>
  297	var_type(Namespace,Array,[array,Type]),
  298	var_type(Namespace,Var,Type),
  299	type_inference_(Namespace,Body).
  300
  301type_inference(Namespace,foreach_with_index(Array,Var,Index,Type,Body)) <=>
  302	writeln('infer foreach'),
  303	var_type(Namespace,Index,int),
  304	var_type(Namespace,Var,Type),
  305	var_type(Namespace,Array,[array,Type]),
  306	type_inference_(Namespace,Body).
  307
  308type_inference(Namespace,elif(A,B)) <=>
  309    var_type(Namespace,A,bool),
  310    type_inference_(Namespace,B).
  311 
  312type_inference(Namespace,else(A)) <=>
  313    type_inference_(Namespace,A).
  314 
  315type_inference(Namespace,statement_with_semicolon(A))